// pages/approve/approve.js
const app = getApp()
const pathUrl = app.globalData.pathUrl;
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '', // 产品ID
    product: '', // 产品信息
    minPrice: '', // 最低贷款额度
    maxPrice: '', // 最高贷款额度

    proAry: ['填写申请', '提交资料', '提交申请'],

    pickerIndex: '0', // 选择借款期数index
    orderPeriod: '12', // 借款期数
    array: ['12', '18', '24', '36'],

    pickerIndex1: '0', // 选择借款用途index
    orderPeriod1: '个人日常消费', // 借款用途
    array1: ['个人日常消费', '装修', '旅游', '教育', '医疗'],

    pickerIndex2: '0', // 选择合作渠道途index
    array2: ['B01', 'B02', 'B03', 'B04', 'B05'],
    query12: 'B01', // 合作渠道

    query09: '', // 申请金额

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    let that = this;
    var loginInfo = JSON.parse(wx.getStorageSync('loginInfo'));
    that.setData({
      id: options.id,
      myInfo: loginInfo
    });
    this.getDetail();
  },
  // 获取产品详情
  getDetail: function() {
    var that = this;
    console.log(that.data.id);
    wx.request({
      url: pathUrl + '/xdbase/fxjBase01/queryById',
      data: {
        id: that.data.id
      },
      success: function(res) {
        console.log(res);
        console.log(res.data.result);
        if (res.data.success) {
          let price = (res.data.result.query08).split('~');

          that.setData({
            product: res.data.result,
            minPrice: price[0], // 最低贷款额度
            maxPrice: price[1] // 最高贷款额度
          });

        }
      },
      fail: function(res) {}
    });
  },
  // 选择借款期限
  bindcash: function(e) {
    var index = e.detail.value;
    var orderPeriod = this.data.array[index];
    this.setData({
      pickerIndex: index,
      orderPeriod: orderPeriod
    })
  },
  // 申请金额
  query09Input: function(e) {
    if (Number(e.detail.value) > Number(this.data.maxPrice)) {
      wx.showModal({
        title: '提示',
        content: '借款金额不大于贷款额度',
      });
      this.setData({
        query09: this.data.maxPrice
      });
    } else {
      this.setData({
        query09: e.detail.value
      });
    }
  },
  // 合作商户
  query12Input: function(e) {
    this.setData({
      query12: e.detail.value
    });
  },

  // 选择借款用途
  bindcash1: function(e) {
    var index1 = e.detail.value;
    var orderPeriod1 = this.data.array1[index1];
    this.setData({
      pickerIndex1: index1,
      orderPeriod1: orderPeriod1
    });
  },
  // 选择合作渠道
  bindcash2: function(e) {
    var index2 = e.detail.value;
    var orderPeriod2 = this.data.array2[index2];
    this.setData({
      pickerIndex2: index2,
      query12: orderPeriod2
    });
  },

  // 下一步
  shenqing: function(e) {
    var loginInfo = wx.getStorageSync('loginInfo');
    if (!loginInfo) { // 未登陆
      wx.showModal({
        title: '提示',
        content: '您还未登陆，请登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../login/login',
            });
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    } else {
      if (!this.data.query09) {
        wx.showModal({
          title: '提示',
          content: '请填写借款金额',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../login/login',
              });
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        return false;
      }
      if (Number(this.data.query09) < Number(this.data.minPrice)) {
        var that = this;
        wx.showModal({
          title: '提示',
          content: '借款金额不小于贷款额度',
          success(res) {
            if (res.confirm) {

              that.setData({
                query09: that.data.minPrice
              });
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        });
        return false;
      }

      let myInfo = JSON.parse(loginInfo);
      console.log(myInfo.records[0].query13)
      let data01 = {
        query05: this.data.product.query03, // 申请产品
        query06: myInfo.records[0].query12, // 电话
        query07: myInfo.records[0].query13, // 客户姓名
        query09: this.data.query09, // 申请金额
        query11: util.formatTime(new Date()), // 申请时间
        query12: this.data.query12, // 合作渠道
        query16: this.data.orderPeriod, // 借款期数
        query17: this.data.orderPeriod1, // 借款用途
        fxjBuse0110List: [{
          query03: "", // 订单编号
          query04: "", // 订单状态
          query05: this.data.product.query03, // 申请产品
          query06: myInfo.records[0].query12, //电话
          query07: myInfo.records[0].query13, // 客户姓名
          query08: "", // 身份证
          query09: this.data.query09, // 申请金额
          query10: this.data.product.query11, // 申请利率
          query11: this.data.product.query09, // 还款方式
          query12: this.data.orderPeriod, // 申请期限
          query13: this.data.query12, // 渠道代码
          query14: "", // 主借人姓名
          query15: "", // 联系电话
          query16: "", // 身份证号
          query17: "", // 配偶姓名
          query18: "", // 配偶电话
          query19: "", // 配偶身份证
          query20: "", // 共借人姓名
          query21: "", // 共借人电话
          query22: "", // 共借人身份证
          query23: "", // 主借人单位
          query24: "", // 主借人部门
          query25: "", // 主借人职位
          query26: "", // 主借人单位电话
          query27: "", // 主借人地址
          query28: "", // 开户行
          query29: "" // 银行卡号
        }],
        fxjBuse0120List: [{
          query03: "",
          query04: "",
          query05: "",
          query06: ""
        }],
        fxjBuse0130List: [{
          query03: "",
          query04: "",
          query05: "",
          query06: "",
          query07: "",
          query08: ""
        }],
        fxjBuse0140List: [{
          query03: "",
          query04: "",
          query05: "",
          query06: "",
          query07: "",
          query08: "",
          query09: ""
        }],
        fxjBuse0150List: [{
          query03: "",
          query04: "",
          query05: "",
          query06: "",
          query07: ""
        }],
        fxjBuse0160List: [{
          query03: "",
          query04: "",
          query05: "",
          query06: "",
          query07: ""
        }],
      }
      wx.showLoading({
        title: '正在提交',
      });
      console.log(data01);
      wx.request({
        url: pathUrl + '/xdbuse/fxjBuse01/add',
        method: 'POST',
        data: data01,
        success: function(res) {
          console.log(res);
          console.log(data01)
          if (!res.data.success) {
            wx.showToast({
              title: '提交失败',
              icon: 'fail',
              duration: 1000
            });
          } else {
            wx.hideLoading();
            wx.navigateTo({
              url: '../subImformation/subImformation?product=' + data01.query05 + '&name=' + data01.query07 + '&query09=' + data01.query09 + '&orderPeriod=' + data01.query16 + '&orderPeriod1=' + data01.query17 + '&hzqd=' + data01.query12
            });
          }
        },
        fail: function(res) {}
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})